﻿using System;
using System.Windows;
using System.Windows.Controls;
using FirstFloor.ModernUI.Windows.Controls;
using IsikFx.Project.ClientApp.Helpers;
using IsikFx.Project.CommonTypes;
using System.Collections.Generic;
using IsikFx.Project.CommonTypes.Announcement;
using IsikFx.Project.CommonTypes.User;
using System.Diagnostics;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using IsikFx.Project.CommonTypes.Watchman;
namespace IsikFx.Project.ClientApp.Pages
{
    /// <summary>
    /// Interaction logic for Home.xaml
    /// </summary>
    public partial class Home : UserControl
    {
        static bool isSplashShown = false;

        public Home()
        {
            InitializeComponent();

            Loaded += Home_Loaded;
        }

        void Home_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeCurrentUser();
            InitProfile();
            InitAnnouncementList();
            InitWatchmenList(Helper.CurrentUser.RoleType);
        }

        #region ### INITIALIZERS ###
        private void InitWatchmenList(int RoleType)
        {
            stckWatchers.Visibility = System.Windows.Visibility.Hidden;
            if (RoleType == ((int)Helper.Roles.Retention))
            {
                stckWatchers.Visibility = System.Windows.Visibility.Hidden;
                return;
            }
            else
            {
                stckWatchers.Visibility = System.Windows.Visibility.Visible;
            }
            WatchmanResponseMessage watchmanResponseMsg = Helper.GetWatchmenList(DateTime.Today.ToLongDateString());
            List<Watchman> listToManipulate = watchmanResponseMsg.Response.WatchmanList;
            List<Watchman> manipulatedList = new List<Watchman>();
            manipulatedList = listToManipulate;
            listToManipulate.CopyTo(manipulatedList.ToArray());
            foreach (Watchman watchman in manipulatedList)
            {
                watchman.WatchmanName = watchman.WatchmanName + " " + watchman.WatchmanSurname;
            }

            watcherList.ItemsSource = manipulatedList;
        }

        private void InitAnnouncementList()
        {
            AnnouncementResponseMessage responseMsg = Helper.GetActiveAnnouncementsAsUser(Helper.CurrentUser.RoleType);
            if (responseMsg.HasError)
            {
                ModernDialog dialog = new ModernDialog();
                dialog.Content = responseMsg.ErrorMessage;
                dialog.ShowDialog();
            }
            else
            {
                if (responseMsg.Response != null && responseMsg.Response.AnnouncementArray != null && responseMsg.Response.AnnouncementArray.Count > 0)
                {
                    Announcements.ItemsSource = responseMsg.Response.AnnouncementArray;
                }
            }
        }

        private void InitProfile()
        {
            try
            {
                string tempPath = System.IO.Path.GetTempPath();
                string photoFilePath = System.Configuration.ConfigurationManager.AppSettings["PhotoFilePath"] as string;
                string fileName = Helper.CurrentUser.Name + "_" + Helper.CurrentUser.Surname;

                BitmapImage img = new BitmapImage();
                img.BeginInit();
                img.CacheOption = BitmapCacheOption.OnLoad;
                img.UriSource = new Uri(System.IO.Path.Combine(tempPath, photoFilePath, fileName) + ".jpg", UriKind.Absolute);
                img.EndInit();
                photo.Source = img;
                NameSurname.Text = Helper.CurrentUser.Name + " " + Helper.CurrentUser.Surname;
            }
            catch (Exception ex)
            {

            }
        }

        private void InitializeCurrentUser()
        {
            try
            {
                Helper.InitializeCurrentUser();
                ModernDialog wellcomeDialog = new ModernDialog();
                if (!isSplashShown)
                {
                    wellcomeDialog.Content = Helper.CurrentUser.Name + " " + Helper.CurrentUser.Surname + " :)";
                    wellcomeDialog.Title = "Hoşgeldiniz.";
                    wellcomeDialog.ShowDialog();
                    isSplashShown = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                System.Threading.Thread.Sleep(3000);
                Process.GetCurrentProcess().Kill();
            }
        }
        #endregion
    }
}
